﻿Imports DTO
Imports BUS
Public Class FormPhanQuyen

    Public _flag As Integer = 0
    Public _maLoaiTK As Integer = 0
    Public _loadDefault As Integer = 0 'Kiểm tra phải là lần đầu load ko
    Dim _ctQuyenBUS As New ChiTietQuyenBUS()
    Dim _loaiTKBUS As New LoaiTaiKhoanBUS()
    Dim _quyenBUS As New QuyenBUS()
    Private Sub FormPhanQuyen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDSLoaiTK()
    End Sub

    Public Sub LoadDSLoaiTK()

        lvLoaiTK.Items.Clear()

        Dim _listLoaiTK As New List(Of LoaiTaiKhoanDTO)
        _listLoaiTK = _loaiTKBUS.LayDSLoaiTaiKhoanDAO()
        Dim stt As Integer = 1
        For Each _loaiTK As LoaiTaiKhoanDTO In _listLoaiTK

            If (_loaiTK.MaLoaiTK <> 1) Then
                Dim item As New ListViewItem()
                item.Text = _loaiTK.MaLoaiTK

                item.SubItems.Add(stt)

                item.SubItems.Add(_loaiTK.TenLoai)

                lvLoaiTK.Items.Add(item)
                stt += 1
            End If
            
        Next
    End Sub

    Public Sub LoadQuyen(ByVal _maLoaiTK)
        If (_maLoaiTK = 0) Then
            Return
        End If
        Dim _listQuyen As New List(Of ChiTietQuyenDTO)
        _listQuyen = _ctQuyenBUS.LayDSCTQuyenTuMaLoaiTKBUS(_maLoaiTK)

        _loadDefault = 0

        For i = 0 To gpQuyen.Controls.Count - 1                        
            CType(gpQuyen.Controls(i), DevComponents.DotNetBar.Controls.CheckBoxX).Checked = False
        Next

        For Each _ctuyen As ChiTietQuyenDTO In _listQuyen
            For i = 0 To gpQuyen.Controls.Count - 1
                Dim _tenCB As String = gpQuyen.Controls(i).Name
                If (_tenCB.Contains(_ctuyen.Quyen)) Then
                    CType(gpQuyen.Controls(i), DevComponents.DotNetBar.Controls.CheckBoxX).Checked = True
                End If
            Next
        Next
        _loadDefault = 1


    End Sub

    Private Sub lvLoaiTK_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvLoaiTK.SelectedIndexChanged
        If (lvLoaiTK.SelectedItems.Count = 0) Then
            _maLoaiTK = 0
            Return
        End If
        Dim _item As ListViewItem = lvLoaiTK.SelectedItems(0)
        _maLoaiTK = Integer.Parse(_item.Text)

        LoadQuyen(_maLoaiTK)

    End Sub

    Private Sub btnDong_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDong.Click
        Me.Close()
    End Sub

    Private Sub cbbbitemDSBanBida_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbbitemDSBanBida.CheckedChanged
        If (_loadDefault = 0) Then
            Return
        End If
        If (_maLoaiTK = 0) Then
            Return
        End If
        If (cbbbitemDSBanBida.Checked = True) Then
            _quyenBUS.SetQuyenBUS("bbitemDSBanBida", _maLoaiTK, 1)
        Else
            _quyenBUS.SetQuyenBUS("bbitemDSBanBida", _maLoaiTK, 0)
        End If
    End Sub


    Private Sub cbbbitemThemBan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbbitemThemBan.CheckedChanged
        If (_loadDefault = 0) Then
            Return
        End If
        If (_maLoaiTK = 0) Then
            Return
        End If
        If (cbbbitemThemBan.Checked = True) Then
            _quyenBUS.SetQuyenBUS("bbitemThemBan", _maLoaiTK, 1)
        Else
            _quyenBUS.SetQuyenBUS("bbitemThemBan", _maLoaiTK, 0)
        End If
    End Sub

    Private Sub cbbbitemSuaBan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbbitemSuaBan.CheckedChanged
        If (_loadDefault = 0) Then
            Return
        End If
        If (_maLoaiTK = 0) Then
            Return
        End If
        If (cbbbitemSuaBan.Checked = True) Then
            _quyenBUS.SetQuyenBUS("bbitemSuaBan", _maLoaiTK, 1)
        Else
            _quyenBUS.SetQuyenBUS("bbitemThemBan", _maLoaiTK, 0)
        End If

    End Sub

    Private Sub cbbbitemXoaBan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbbitemXoaBan.CheckedChanged

        If (_loadDefault = 0) Then
            Return
        End If
        If (_maLoaiTK = 0) Then
            Return
        End If

        If (cbbbitemXoaBan.Checked = True) Then
            _quyenBUS.SetQuyenBUS("bbitemXoaBan", _maLoaiTK, 1)
        Else
            _quyenBUS.SetQuyenBUS("bbitemXoaBan", _maLoaiTK, 0)
        End If
    End Sub

    Private Sub cbbbitemQLHoaDon_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbbitemQLHoaDon.CheckedChanged

        If (_loadDefault = 0) Then
            Return
        End If
        If (_maLoaiTK = 0) Then
            Return
        End If

        If (cbbbitemQLHoaDon.Checked = True) Then
            _quyenBUS.SetQuyenBUS("bbitemQLHoaDon", _maLoaiTK, 1)
        Else
            _quyenBUS.SetQuyenBUS("bbitemQLHoaDon", _maLoaiTK, 0)
        End If
    End Sub

    Private Sub cbbbitemDSNhanVien_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbbitemDSNhanVien.CheckedChanged

        If (_loadDefault = 0) Then
            Return
        End If
        If (_maLoaiTK = 0) Then
            Return
        End If

        If (cbbbitemDSNhanVien.Checked = True) Then
            _quyenBUS.SetQuyenBUS("bbitemDSNhanVien", _maLoaiTK, 1)
        Else
            _quyenBUS.SetQuyenBUS("bbitemDSNhanVien", _maLoaiTK, 0)
        End If
    End Sub

    Private Sub cbbbitemQLCaTruc_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbbitemQLCaTruc.CheckedChanged
        If (_loadDefault = 0) Then
            Return
        End If
        If (_maLoaiTK = 0) Then
            Return
        End If

        If (cbbbitemQLCaTruc.Checked = True) Then
            _quyenBUS.SetQuyenBUS("bbitemQLCaTruc", _maLoaiTK, 1)
        Else
            _quyenBUS.SetQuyenBUS("bbitemQLCaTruc", _maLoaiTK, 0)
        End If
    End Sub

    Private Sub cbbbitemDoanhThuThang_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbbitemDoanhThuThang.CheckedChanged
        If (_loadDefault = 0) Then
            Return
        End If
        If (_maLoaiTK = 0) Then
            Return
        End If

        If (cbbbitemDoanhThuThang.Checked = True) Then
            _quyenBUS.SetQuyenBUS("bbitemDoanhThuThang", _maLoaiTK, 1)
        Else
            _quyenBUS.SetQuyenBUS("bbitemDoanhThuThang", _maLoaiTK, 0)
        End If
    End Sub

    Private Sub cbbbitemDoanhThuQuy_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbbitemDoanhThuQuy.CheckedChanged
        If (_loadDefault = 0) Then
            Return
        End If
        If (_maLoaiTK = 0) Then
            Return
        End If

        If (cbbbitemDoanhThuQuy.Checked = True) Then
            _quyenBUS.SetQuyenBUS("bbitemDoanhThuQuy", _maLoaiTK, 1)
        Else
            _quyenBUS.SetQuyenBUS("bbitemDoanhThuQuy", _maLoaiTK, 0)
        End If
    End Sub

    Private Sub cbbbitemDoanhThuNam_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbbitemDoanhThuNam.CheckedChanged
        If (_loadDefault = 0) Then
            Return
        End If
        If (_maLoaiTK = 0) Then
            Return
        End If

        If (cbbbitemDoanhThuNam.Checked = True) Then
            _quyenBUS.SetQuyenBUS("bbitemDoanhThuNam", _maLoaiTK, 1)
        Else
            _quyenBUS.SetQuyenBUS("bbitemDoanhThuNam", _maLoaiTK, 0)
        End If
    End Sub

    Private Sub cbbbitemLuongTungNV_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbbitemLuongTungNV.CheckedChanged
        If (_loadDefault = 0) Then
            Return
        End If
        If (_maLoaiTK = 0) Then
            Return
        End If

        If (cbbbitemLuongTungNV.Checked = True) Then
            _quyenBUS.SetQuyenBUS("bbitemLuongTungNV", _maLoaiTK, 1)
        Else
            _quyenBUS.SetQuyenBUS("bbitemLuongTungNV", _maLoaiTK, 0)
        End If
    End Sub

    Private Sub cbbbitemLuongToanNV_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbbitemLuongToanNV.CheckedChanged
        If (_loadDefault = 0) Then
            Return
        End If
        If (_maLoaiTK = 0) Then
            Return
        End If

        If (cbbbitemLuongToanNV.Checked = True) Then
            _quyenBUS.SetQuyenBUS("bbitemLuongToanNV", _maLoaiTK, 1)
        Else
            _quyenBUS.SetQuyenBUS("bbitemLuongToanNV", _maLoaiTK, 0)
        End If
    End Sub

    Private Sub cbbbitemDangKy_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbbitemDangKy.CheckedChanged
        If (_loadDefault = 0) Then
            Return
        End If
        If (_maLoaiTK = 0) Then
            Return
        End If

        If (cbbbitemDangKy.Checked = True) Then
            _quyenBUS.SetQuyenBUS("bbitemDangKy", _maLoaiTK, 1)
        Else
            _quyenBUS.SetQuyenBUS("bbitemDangKy", _maLoaiTK, 0)
        End If
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click

        If (txtTenLoaiTK.Text = "") Then
            MessageBox.Show("Hãy nhập tên loại tài khoản")
            Return
        End If

        Dim _listLoaiTK As New List(Of LoaiTaiKhoanDTO)

        _listLoaiTK = _loaiTKBUS.LayDSLoaiTaiKhoanDAO()
        For Each _loaiTK As LoaiTaiKhoanDTO In _listLoaiTK
            If (_loaiTK.TenLoai = txtTenLoaiTK.Text) Then
                MessageBox.Show("Tên loại tài khoản đã trùng")
                Return
            End If
        Next

        _loaiTKBUS.ThemLoaiTKBUS(txtTenLoaiTK.Text)
        LoadDSLoaiTK()
    End Sub

    Private Sub lvLoaiTK_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles lvLoaiTK.KeyPress

    End Sub

    Private Sub lvLoaiTK_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lvLoaiTK.KeyDown
        If (lvLoaiTK.SelectedItems.Count > 0) Then
            If (e.KeyCode = Keys.Delete) Then
                Dim value As Integer = MessageBox.Show("Bạn có chắc muốn xoá" & vbNewLine & "Xoá loại tài khoản sẽ ảnh hưởng đến các acc hiện tại", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If (value = 6) Then
                    Dim _item As ListViewItem = lvLoaiTK.SelectedItems(0)
                    _loaiTKBUS.XoaLoaiTKBUS(_item.Text)
                    LoadDSLoaiTK()
                End If
            End If
        End If
    End Sub
End Class